package com.hzy.listview;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hzy.login.UserDBHelper;
import com.hzy.text.R;
import com.sam.dialog.CustomDialog;
import com.sam.widget.text_view.CustomFontTextView;

import java.util.List;

/**
 * Created by Administrator on 2016/6/24 0024.
 */
public class NotepadRecordAdapter extends BaseAdapter {
    private Context context;
    private String userName;
    private List<NotepadRecordEntity> notepadRecordList;
    private UserDBHelper userDBHelper;

    public NotepadRecordAdapter(Context context,List<NotepadRecordEntity> notepadRecordList,String userName) {
        this.notepadRecordList = notepadRecordList;
        this.context = context;
        this.userName = userName;
    }

    @Override
    public int getCount() {
        return notepadRecordList.size();
    }

    @Override
    public Object getItem(int position) {
        return notepadRecordList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = View.inflate(context, R.layout.notepad_list_item,null);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.record_title);
            viewHolder.dateTime = (TextView) convertView.findViewById(R.id.dateTime);
            viewHolder.delectBtn = (ImageView) convertView.findViewById(R.id.delect_item);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        userDBHelper =new UserDBHelper(context,"User.db",null,1);
        final SQLiteDatabase db = userDBHelper.getWritableDatabase();


        viewHolder.title.setText(notepadRecordList.get(position).getNoteTitle());
        viewHolder.dateTime.setText(notepadRecordList.get(position).getDateTime());

        viewHolder.delectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CustomDialog removeRecordDialog = new CustomDialog(context,R.layout.dialog_remove_record_dialog);
                removeRecordDialog.findViewById(R.id.note_confirm_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        userDBHelper =new UserDBHelper(context,"User.db",null,1);
//                        SQLiteDatabase db = userDBHelper.getWritableDatabase();
                        db.delete("Note","userName = ? and noteTitle = ?",new String[]{userName,notepadRecordList.get(position).getNoteTitle()});
                        notepadRecordList.remove(position);
                        notifyDataSetChanged();
                        removeRecordDialog.dismiss();
                    }
                });
                removeRecordDialog.findViewById(R.id.note_cancel_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeRecordDialog.dismiss();
                    }
                });
                removeRecordDialog.show();
            }
        });
        return convertView;
    }

    private class ViewHolder {
        TextView title;
        TextView dateTime;
        ImageView delectBtn;
    }
}
